package com.ruoyi.business.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.qrcode.BufferedImageLuminanceSource;
import cn.hutool.http.HttpUtil;
import com.google.zxing.*;
import com.google.zxing.common.HybridBinarizer;
import com.ruoyi.system.service.ISysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Component
public abstract class BasePayment implements PaymentInterface{
    @Resource
    public ITRecipientInfoService userRecipientService;

    @Resource
    public ITOrderInfoService orderInfoService;

    @Autowired
    public ISysConfigService sysConfigService;

    @Resource
    public LoginService loginService;

    public Logger logger =  LoggerFactory.getLogger(this.getClass());

    @Resource
    public CommonService commonService;


    public static String getParamByUrl(String url, String name) {

        String query = StrUtil.subAfter(url, "?", false);

        // 使用Hutool的MapUtil来转换参数为Map
        Map<String, String> paramMap = HttpUtil.decodeParamMap(query,"UTF-8");

        // 获取特定的参数
        String param1 = paramMap.get(name);

        // 打印结果
        System.out.println("param1: " + param1);
        return  param1;
    }

    public static String  decodeQR(String filePath) {
//        String filePath = "E:/txdata/data/images/qrCode.jpg";//二维码路径
        BufferedImage image;
        Result result = null;
        try {
            image = ImageIO.read(new File(filePath));
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码
            System.out.println("图片中内容：  ");
            System.out.println("author： " + result.getText());
            System.out.println("图片中格式：  ");
            System.out.println("encode： " + result.getBarcodeFormat());
            return result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
